<?php

namespace app\shopapi\model\pay;

use fast\Http;


//支付宝商家认证
class Alipay
{

    public $agent_no =  '84822596';
    public $secret_key = '3fb101f9512b47de0bf6a36ef524b518';


    //获取支付宝认证码
    public function authcode($merchant_no)
    {
        $api = "https://test.esw235.com/unipay/enter/alApplyment";

        $data = [
            'merchant_no'=>$merchant_no
        ];

        $pre_data = $this->signature($data);
        dump($pre_data);
        $res = Http::sendRequest($api,$pre_data);

        $result = json_decode($res['msg'],true);
        dump($result);die;
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }


    //查询认证状态
    public function authCodeStatus($merchant_no)
    {
        $api = "https://test.esw235.com/unipay/enter/alApplymentState";

        $data = [
            'merchant_no'=>$merchant_no
        ];

        $pre_data = $this->signature($data);
        dump($pre_data);
        $res = Http::sendRequest($api,$pre_data);

        $result = json_decode($res['msg'],true);
        dump($result);die;
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    //撤销申请单
    public function recall($merchant_no)
    {
        $api = "https://test.esw235.com/unipay/enter/alApplymentCancel";

        $data = [
            'merchant_no'=>$merchant_no
        ];

        $pre_data = $this->signature($data);
        dump($pre_data);
        $res = Http::sendRequest($api,$pre_data);

        $result = json_decode($res['msg'],true);
        dump($result);die;
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 签名
     * @param array $data
     * @return array
     * @author cfn <cfn@leapy.cn>
     * @date 2021/11/9
     */
    protected function signature(array $data)
    {
        $pre_data = [
            'data' => json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'agent_no' => $this->agent_no,
            'nonce' => self::getRandomStr(16),
            'timestamp' => time(),
            'version' => '1.0',
        ];
        $pre_data['signature'] = self::createSignature($pre_data);
        return $pre_data;
    }

    /**
     * 创建签名字符串
     * @param $data
     * @return string
     * @author cfn <cfn@leapy.cn>
     * @date 2021/11/9
     */
    protected function createSignature($data)
    {
        // 1. 预签名数组 注意按照ascii排序
        ksort($data);
        // 2. 对数据进行k=v拼接并用&连接,内容为空的除外，例如：a=1&b=2
        $pre_str = '';
        foreach($data as $k => $v)
        {
            // 空值不参与签名
            !empty($v) && $pre_str .= $k . "=" . $v . "&";
        }
        // 3. 将auth_key拼接到最后边
        $pre_str .= 'secret_key='.$this->secret_key;
        // 4. 对数据进行md5加密，并转大写
        return strtoupper(md5($pre_str));
    }

    /**
     * 随机字符串
     * @param $len
     * @return string
     * @author cfn <cfn@leapy.cn>
     * @date 2021/11/9
     */
    protected function getRandomStr($len)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }
}